package com.point_of_sale.tests;

import static org.junit.Assert.*;

import java.util.HashMap;

import org.junit.Test;

import com.point_of_sale.product.NormalProduct;
import com.point_of_sale.product.Product;
import com.point_of_sale.product.WholesaleProduct;
import com.point_of_sale.product_loader.PlainTextProductLoader;

// This is a simple test of the PlainTextProductLoader class to make sure it loaded the data in correctly
public class PlainTextLoaderTest 
{

	@Test
	public void testGetProducts() 
	{
		HashMap<String, Product> testProducts = new HashMap<String, Product>();
		
		PlainTextProductLoader loader = new PlainTextProductLoader();
		HashMap<String, Product> actualProducts = loader.getProducts("bin/com/point_of_sale/assets/product_list.txt");
		
		WholesaleProduct p1 = new WholesaleProduct();
		p1.setPrice(2.00f);
		p1.setProductCode("A");
		p1.setWholesaleNumber(4);
		p1.setWholesalePrice(7.00f);
		
		testProducts.put(p1.getProductCode(), p1);
		
		NormalProduct p2 = new NormalProduct();
		p2.setPrice(12.00f);
		p2.setProductCode("B");
		
		testProducts.put(p2.getProductCode(), p2);
		
		WholesaleProduct p3 = new WholesaleProduct();
		p3.setPrice(1.25f);
		p3.setProductCode("C");
		p3.setWholesaleNumber(6);
		p3.setWholesalePrice(6.00f);
		
		testProducts.put(p3.getProductCode(), p3);
		
		NormalProduct p4 = new NormalProduct();
		p4.setPrice(0.15f);
		p4.setProductCode("D");
		
		testProducts.put(p4.getProductCode(), p4);
		
		assertEquals(testProducts.keySet().size(), actualProducts.keySet().size());
		assertEquals(testProducts.keySet(), actualProducts.keySet());
		
		for(String key : testProducts.keySet())
		{
			// I had trouble getting the Product's .equals() method to be picked up so I'm doing it all out
			// I would fix this if I had more time
			assertEquals(testProducts.get(key).getProductCode(), actualProducts.get(key).getProductCode());
			assertEquals(testProducts.get(key).getPrice(), actualProducts.get(key).getPrice());
			
			// Two extra equality checks for WholesaleProduct
			if(testProducts.get(key) instanceof WholesaleProduct)
			{
				assertEquals(((WholesaleProduct)testProducts.get(key)).getWholesalePrice(), 
						((WholesaleProduct) actualProducts.get(key)).getWholesalePrice());
				assertEquals(((WholesaleProduct)testProducts.get(key)).getWholesaleNumber(), 
						((WholesaleProduct) actualProducts.get(key)).getWholesaleNumber());
			}
		}	
	}
}
